package com.ruoyi.system.service.impl;

import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.domain.CityDictionary;
import com.ruoyi.system.domain.SysDistrict;
import com.ruoyi.system.domain.vo.DictRelevanceVo;
import com.ruoyi.system.mapper.CityDictionaryMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDistrictMapper;
import com.ruoyi.system.service.ICityDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典关联城市Service业务层处理
 *
 * @author MagicCube
 * @date 2023-06-09
 */
@Service
public class CityDictionaryServiceImpl implements ICityDictionaryService {
    @Autowired
    private CityDictionaryMapper cityDictionaryMapper;

    @Autowired
    private SysDistrictMapper sysDistrictMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询字典关联城市
     *
     * @param id 字典关联城市主键
     * @return 字典关联城市
     */
    @Override
    public CityDictionary selectCityDictionaryById(Long id) {
        return cityDictionaryMapper.selectCityDictionaryById(id);
    }

    @Override
    public DictRelevanceVo selectDictCityInfo(DictRelevanceVo dictRelevanceVo) {
        DictRelevanceVo relevanceVo = new DictRelevanceVo();
        relevanceVo.setDictionaryId(dictRelevanceVo.getDictionaryId());
//        if (dictRelevanceVo.getRelevanceType() != null && dictRelevanceVo.getRelevanceType().equals("0")) {
//            List<Long> dictIds = cityDictionaryMapper.selectCityInfoById(relevanceVo);
////            SysDistrict sysDistrict = sysDistrictMapper.selectSysDistrictById(relevanceVo.getCityId());
////            relevanceVo.setCity(sysDistrict.getName());
//            relevanceVo.setDictionaryIds(dictIds);
//            List<String> stringList = cityDictionaryMapper.selectDictTypeListByDictIds(dictIds);
//            relevanceVo.setDictionaryList(stringList);
//            relevanceVo.setDictionary(stringList.toString());
//            relevanceVo.setRelevanceType("0");
//        } else if (dictRelevanceVo.getRelevanceType() != null && dictRelevanceVo.getRelevanceType().equals("1")) {
        List<String> cityIds = cityDictionaryMapper.selectDictInfoById(dictRelevanceVo.getDictionaryId());
        SysDictData sysDictData = sysDictDataMapper.selectDictDataById(dictRelevanceVo.getDictionaryId());
//        List<String> list1 = cityDictionaryMapper.selectDictInfoById(relevanceVo.getDictionaryId());
        List<String> stringList = new ArrayList<>();
        List<List<String>> list = new ArrayList<>();
        for (String s : cityIds) {
            String[] split = s.replace("[", "").replace("]", "").split(", ");
            list.add(Arrays.asList(split));
            String s1 = cityDictionaryMapper.selectDictTypeListByCityIds(Arrays.asList(split));
            stringList.add(s1.replace(",", "/"));
        }
        relevanceVo.setDictionary(sysDictData.getDictLabel());
        relevanceVo.setCityIds(list);
//            List<String> stringList = cityDictionaryMapper.selectDictTypeListByCityIds(cityIds);
        relevanceVo.setCityList(stringList);
        relevanceVo.setCity(stringList.toString());
        relevanceVo.setRelevanceType("1");
//        }
        return relevanceVo;
    }

    /**
     * 查询字典关联城市列表
     *
     * @param cityDictionary 字典关联城市
     * @return 字典关联城市
     */
    @Override
    public List<CityDictionary> selectCityDictionaryList(CityDictionary cityDictionary) {
        return cityDictionaryMapper.selectCityDictionaryList(cityDictionary);
    }

    @Override
    public List<DictRelevanceVo> selectCityDictRelevanceList(DictRelevanceVo dictRelevanceVo) {
//        if (dictRelevanceVo.getRelevanceType() != null && dictRelevanceVo.getRelevanceType().equals("0")) {
//            List<DictRelevanceVo> cityList = cityDictionaryMapper.GroupByCity(dictRelevanceVo);
//            for (DictRelevanceVo relevanceVo : cityList) {
//                List<Long> list = new ArrayList<>();
//                for (String s : Arrays.asList(relevanceVo.getDictionary().split(","))) {
//                    list.add(Long.valueOf(s));
//                }
////                SysDistrict sysDistrict = sysDistrictMapper.selectSysDistrictById(relevanceVo.getCityId());
////                relevanceVo.setCity(sysDistrict.getName());
//                relevanceVo.setDictionaryIds(list);
//                List<String> stringList = cityDictionaryMapper.selectDictTypeListByDictIds(list);
//                relevanceVo.setDictionaryList(stringList);
//                relevanceVo.setDictionary(stringList.toString());
//                relevanceVo.setRelevanceType("0");
//            }
////            List<CityDictionary> dictionaries = cityDictionaryMapper.selectCityDictListByCityIds(cityList);
////            Map<Long, Map<Long, List<CityDictionary>>> collect = dictionaries.stream().collect(Collectors.groupingBy(CityDictionary::getCityId, Collectors.groupingBy(CityDictionary::getDictionaryTypeId)));
////            for (DictRelevanceVo relevanceVo : cityList) {
////                for (Map.Entry<Long, Map<Long, List<CityDictionary>>> mapEntry : collect.entrySet()) {
////                    for (Map.Entry<Long, List<CityDictionary>> listEntry : mapEntry.getValue().entrySet()) {
////                        if (Objects.equals(mapEntry.getKey(), relevanceVo.getCityId()) && Objects.equals(listEntry.getKey(), relevanceVo.getDictionaryTypeId())){
////                            relevanceVo.s
////                        }
////                    }
////                }
////            }
//            return cityList;
//        } else if (dictRelevanceVo.getRelevanceType() != null && dictRelevanceVo.getRelevanceType().equals("1")) {
        //查询关联经济带类型


        SysDictData dictData = new SysDictData();
        List<DictRelevanceVo> dictIds = new ArrayList<>();
        dictData.setDictType("economic_belt");
        for (SysDictData sysDictData : sysDictDataMapper.selectDictDataList(dictData)) {
            List<List<String>> list = new ArrayList<>();
            List<String> cityList = new ArrayList<>();
//                list.addAll();
            List<String> list1 = cityDictionaryMapper.selectDictInfoById(sysDictData.getDictCode());
            List<String> stringList = new ArrayList<>();
            for (String s : list1) {
                String[] split = s.replace("[", "").replace("]", "").split(", ");
                list.add(Arrays.asList(split));
                cityList.add(split[1]);
                String s1 = cityDictionaryMapper.selectDictTypeListByCityIds(Arrays.asList(split));
                stringList.add(s1.replace(",",""));
            }
//            SysDictData sysDictData = sysDictDataMapper.selectDictDataById(sysd);
            DictRelevanceVo relevanceVo = new DictRelevanceVo();
            relevanceVo.setDictionaryId(sysDictData.getDictCode());
            relevanceVo.setDictionaryTypeCode(sysDictData.getDictType());
            relevanceVo.setDictName("经济带");
            relevanceVo.setDictionaryTypeId(102L);
            relevanceVo.setDictionary(sysDictData.getDictLabel());
            relevanceVo.setCityIds(list);
            relevanceVo.setCityIdzs(cityList);
//                List<String> stringList = cityDictionaryMapper.selectDictTypeListByCityIds(list);
            relevanceVo.setCityList(stringList);
            relevanceVo.setCity(stringList.toString());
            dictIds.add(relevanceVo);
//                relevanceVo.setRelevanceType("1");
        }

//        List<DictRelevanceVo> dictIds = cityDictionaryMapper.GroupByDict(dictRelevanceVo);
//            for (DictRelevanceVo relevanceVo : dictIds) {
//                List<List<String>> list = new ArrayList<>();
//                List<String> cityList = new ArrayList<>();
////                list.addAll();
//                List<String> list1 = cityDictionaryMapper.selectDictInfoById(relevanceVo.getDictionaryId());
//                List<String> stringList = new ArrayList<>();
//                for (String s : list1) {
//                    String[] split = s.replace("[", "").replace("]", "").split(", ");
//                    list.add(Arrays.asList(split));
//                    cityList.add(split[1]);
//                    String s1 = cityDictionaryMapper.selectDictTypeListByCityIds(Arrays.asList(split));
//                    stringList.add(s1.replace(",",""));
//                }
//                SysDictData sysDictData = sysDictDataMapper.selectDictDataById(relevanceVo.getDictionaryId());
//                relevanceVo.setDictionary(sysDictData.getDictLabel());
//                relevanceVo.setCityIds(list);
//                relevanceVo.setCityIdzs(cityList);
////                List<String> stringList = cityDictionaryMapper.selectDictTypeListByCityIds(list);
//                relevanceVo.setCityList(stringList);
//                relevanceVo.setCity(stringList.toString());
////                relevanceVo.setRelevanceType("1");
//            }
//            List<CityDictionary> dictionaries = cityDictionaryMapper.selectCityDictListByDictIds(dictIds);
            return dictIds;
//        }
//        return null;
    }

    /**
     * 新增字典关联城市
     *
     * @param cityDictionary 字典关联城市
     * @return 结果
     */
    @Override
    public int insertCityDictionary(CityDictionary cityDictionary) {
        return cityDictionaryMapper.insertCityDictionary(cityDictionary);
    }

    @Override
    public int insertCityDict(DictRelevanceVo dictRelevanceVo) {
        List<CityDictionary> arrayList = new ArrayList<>();
//        if (dictRelevanceVo.getRelevanceType()!=null&&dictRelevanceVo.getRelevanceType().equals("0")){
//            for (Long dictionaryId : dictRelevanceVo.getDictionaryIds()) {
//                CityDictionary cityDictionary = new CityDictionary();
//                cityDictionary.setCityId(String.valueOf(dictRelevanceVo.getCityId()));
//                cityDictionary.setDictionaryId(dictionaryId);
//                cityDictionary.setDictionaryTypeId(dictRelevanceVo.getDictionaryTypeId());
//                cityDictionary.setDictionaryTypeCode(dictRelevanceVo.getDictionaryTypeCode());
//                arrayList.add(cityDictionary);
//            }
//        }else if (dictRelevanceVo.getRelevanceType()!=null&&dictRelevanceVo.getRelevanceType().equals("1")){

            for (List<String> cityId : dictRelevanceVo.getCityIds()) {
                CityDictionary cityDictionary = new CityDictionary();
                cityDictionary.setCityId(String.valueOf(cityId));
                cityDictionary.setDictionaryId(dictRelevanceVo.getDictionaryId());
                cityDictionary.setDictionaryTypeId(dictRelevanceVo.getDictionaryTypeId());
                cityDictionary.setDictionaryTypeCode(dictRelevanceVo.getDictionaryTypeCode());
                arrayList.add(cityDictionary);
            }
//        }
        if (arrayList.size() != 0){
            return cityDictionaryMapper.insertCityDictionarys(arrayList);
        }
        return 0;
    }

    /**
     * 修改字典关联城市
     *
     * @param cityDictionary 字典关联城市
     * @return 结果
     */
    @Override
    public int updateCityDictionary(CityDictionary cityDictionary) {
        return cityDictionaryMapper.updateCityDictionary(cityDictionary);
    }

    @Override
    public int updateCityDict(DictRelevanceVo dictRelevanceVo) {
        //判断修改类型,并删除旧的关联类型
//        if (dictRelevanceVo.getRelevanceType()!=null&&dictRelevanceVo.getRelevanceType().equals("0")){
//            cityDictionaryMapper.deleteCityDictByCityIdAndDictTypeId(dictRelevanceVo.getCityId(),dictRelevanceVo.getDictionaryTypeId());
//        }else if (dictRelevanceVo.getRelevanceType()!=null&&dictRelevanceVo.getRelevanceType().equals("1")){
            cityDictionaryMapper.deleteCityDictByDictDataId(dictRelevanceVo.getDictionaryId());
//        }
        //重新添加新的城市关联
        return insertCityDict(dictRelevanceVo);
    }

    /**
     * 批量删除字典关联城市
     *
     * @param ids 需要删除的字典关联城市主键
     * @return 结果
     */
    @Override
    public int deleteCityDictionaryByIds(Long[] ids) {
        return cityDictionaryMapper.deleteCityDictionaryByIds(ids);
    }

    /**
     * 删除字典关联城市信息
     *
     * @param id 字典关联城市主键
     * @return 结果
     */
    @Override
    public int deleteCityDictionaryById(Long id) {
        return cityDictionaryMapper.deleteCityDictByDictDataId(id);
    }
}
